#pragma once

#ifndef QCEFOSRWIDGET_H
#define QCEFOSRWIDGET_H

#include "QCefWidget.h"
#include "QCefOsrDelegate.h"
#include "QCefOsrClient.h"

class QCefOsrWidget : public QCefWidget
{
    Q_OBJECT
public:
    QCefOsrWidget(QWidget * parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags());
    QCefOsrWidget(const QString & strUrl, QWidget * parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags());
    virtual ~QCefOsrWidget();

public:
    inline void setFocus() { this->setFocus(Qt::OtherFocusReason); }
    void setFocus(Qt::FocusReason reason);

    virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const override;

protected:
    int initOsr();

    virtual int createClient() override;

protected:
    QImage m_image;

    bool m_popupShow;
    QImage m_popupImage;
    QRect m_popupRect;

    QRect m_imeCursorRect;

protected:
    virtual void paintEvent(QPaintEvent * event) override;
    virtual void inputMethodEvent(QInputMethodEvent * event) override;
    virtual void showEvent(QShowEvent *event) override;
    virtual void hideEvent(QHideEvent *event) override;
    virtual void focusInEvent(QFocusEvent *event) override;
    virtual void focusOutEvent(QFocusEvent *event) override;
    virtual void keyPressEvent(QKeyEvent *event) override;
    virtual void keyReleaseEvent(QKeyEvent *event) override;
    virtual void mousePressEvent(QMouseEvent *event) override;
    virtual void mouseReleaseEvent(QMouseEvent *event) override;
    virtual void mouseMoveEvent(QMouseEvent *event) override;
    virtual void mouseDoubleClickEvent(QMouseEvent *event) override;
    virtual void wheelEvent(QWheelEvent *event) override;

protected:
    void sendCefKeyEvent(QKeyEvent* event);
    void sendCefMouseEvent(QMouseEvent *event);

public slots:
    void onPageCursorChange(int type);
    void onPagePopupShow(bool show);
    void onPagePopupSize(QRect rt);
    void onPagePaint(int type, QRegion region, QImage image, int width, int height);
    void onPageAcceleratedPaint(int type, QRegion region, void * shared_handle);
    void onPageScrollOffsetChanged(double x, double y);
    void onPageImeCompositionRangeChanged(int from, int to, QRect rt);
    void onPageTextSelectionChanged(int from, int to, QString strSelectedText);
    void onPageVirtualKeyboardRequested(int input_mode);
    void onPageFocusedEditableNodeChanged(bool focusOnEditableField);

signals:

};

#endif // QCEFOSRWIDGET_H
